"""
Case Type   : 功能测试
Case Name   : 账户被锁定，回收权限成功
Create At   : 2024-11-29
Owner       : wan005
Description :
        1.创建用户
        2.授予用户权限
        3.to user用户锁定，回收用户权限
        4.on user用户锁定，回收用户权限
        5.用户都锁定，回收用户权限
Expect      :
        1.用户创建成功
        2.授权成功
        3.回收成功
        4.回收成功
        5.回收成功
History     :
"""
import os
import unittest
from yat.test import macro
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class Compatibility(unittest.TestCase):
    def setUp(self):

        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.pri_sh = CommonSH('PrimaryDbBmode')
        self.common = Common()
        self.constant = Constant()
        self.user1 = 'user_revoke_001'
        self.user2 = 'user_revoke_002'
        self.user3 = 'user_revoke_003'

    def test_revoke(self):
        text = '-----step1:创建多个普通用户;expect:创建成功-----'
        self.log.info(text)
        sql1 = f'create user {self.user1} password \'{macro.COMMON_PASSWD}\';' \
               f'create user {self.user2} password \'{macro.COMMON_PASSWD}\';' \
               f'create user {self.user3} password \'{macro.COMMON_PASSWD}\';'
        res1 = self.pri_sh.execut_db_sql(sql1)
        self.log.info(res1)
        self.assertEqual(3, res1.count(
            self.constant.CREATE_ROLE_SUCCESS_MSG), f'执行失败: {text}')

        text = '----step2:授予用户权限;expect:成功----'
        self.log.info(text)
        sql2 = f'grant proxy on {self.user1} to {self.user2} with ' \
               f'grant option;' \
               f'grant proxy on {self.user1} to {self.user3};'
        res2 = self.pri_sh.execut_db_sql(sql2)
        self.log.info(res2)
        self.assertEqual(2, res2.count('GRANT ROLE'), f'执行失败: {text}')

        text = '----step3:to user用户锁定，回收用户权限;expect:成功----'
        self.log.info(text)
        sql3 = f'alter user {self.user2} account lock;' \
               f'revoke proxy on {self.user1} from {self.user2};'
        res3 = self.pri_sh.execut_db_sql(sql3)
        self.log.info(res3)
        self.common.equal_sql_mdg(res3, 'ALTER ROLE', 'REVOKE ROLE')

        text = '----step4:on user用户锁定，回收用户权限;expect:成功----'
        self.log.info(text)
        sql4 = f'alter user {self.user1} account lock;' \
               f'revoke proxy on {self.user1} from {self.user3};'
        res4 = self.pri_sh.execut_db_sql(sql4)
        self.log.info(res4)
        self.common.equal_sql_mdg(res4, 'ALTER ROLE', 'REVOKE ROLE')

        text = '----step5:用户都锁定，授予用户权限;expect:成功----'
        self.log.info(text)
        sql5 = f'grant proxy on {self.user1} to {self.user2};' \
               f'revoke proxy on {self.user1} from {self.user2};'
        res5 = self.pri_sh.execut_db_sql(sql5)
        self.log.info(res5)
        self.common.equal_sql_mdg(res5, 'GRANT ROLE', 'REVOKE ROLE')


    def tearDown(self):
        text = '----清理环境----'
        self.log.info(text)
        sql_cle1 = f'drop user {self.user1},{self.user2},{self.user3} cascade;'
        res_cle1 = self.pri_sh.execut_db_sql(sql_cle1)
        self.log.info(res_cle1)
        self.assertEqual(self.constant.DROP_ROLE_SUCCESS_MSG, res_cle1,
                         f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')